package org.itboys.mobile.controller.small;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.commons.CommonConstants;
import org.itboys.framework.spring.controller.BaseController;

import org.itboys.mobile.entity.mongo.SundayNews;


import org.itboys.mobile.service.mongo.weixin.SundayWeiXinNewsService;
import org.itboys.mobile.util.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Auther: 罗尧坤
 * Date: 2018/7/17
 * Description: 小程序_新闻接口
 */

@Controller
@RequestMapping("/sunday/small/news")
@Api(value = "news", description = "新闻")
public class SundaySmallNewsController extends BaseController {

    @Autowired
    private SundayWeiXinNewsService newsService;

    @ApiOperation("【小程序】用户获取新闻详情")
    @RequestMapping(value = "/getDetail", method = {RequestMethod.POST, RequestMethod.GET})
    public void getDetail(HttpServletRequest request,
                            HttpServletResponse response,
                            Model model,
                            @ApiParam(value = "新闻id", required = true) @RequestParam(value = "newsId") Long newsId
    ) {
        try {
            ResponseMessage.success(newsService.findOne(newsId));
        } catch (Exception e) {
            String msg = CommonConstants.ERROMSG;
            Integer code = CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
                //token已过期
            } else if (e.getClass().getSimpleName().equalsIgnoreCase("TokenException")) {
                code = CommonConstants.TOKEN_CODE;
                msg = CommonConstants.TOKENMSG;
            } else {
                logger.error("/sunday/small/news/getDetail---e=" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code, msg);
        }
    }

    @RequestMapping(value = "/select", method = RequestMethod.POST)
    @ApiOperation("【小程序】获取设置了首页推荐的新闻列表, 分页")
    public void select(HttpServletRequest request, HttpServletResponse response,
                       @ApiParam(value = "起始页", required = true)
                       @RequestParam(value = "pageNumber", required = true) String pageNumber
            ,
                       @ApiParam(value = "每页大小", required = true)
                           @RequestParam(value = "pageSize", required = true) String pageSize
    ) {
        try {
            ResponseMessage.success(newsService.select(request));

        } catch (Exception e) {
            String  msg  = CommonConstants.ERROMSG;
            Integer code = CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
                //token已过期
            } else if (e.getClass().getSimpleName().equalsIgnoreCase("TokenException")) {
                code = CommonConstants.TOKEN_CODE;
                msg = CommonConstants.TOKENMSG;
            } else {
                logger.error("/sunday/small/news/select----e=" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code, msg);
        }
    }

}
